Majority Element 2 ​
Majority Element 2 — LeetCode
Find all the elements that appear more than n/3 times in the array. There can be at most two of them.
Approach ​
Moore's Voting Algorithm with verification. Keep two counts and two majority elements. At most, only two elements will be in result. Then use the voting algorithm. If num is in majorityCandidates, increment counters. If not decrement counters. If one counter is 0, replace it with a new candidate.